package org.springmole.web.controller;

import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;



@Controller
public class MainController {

	@RequestMapping(value="/springmole.spy", method=RequestMethod.GET)
	public void processGet(HttpServletRequest request, HttpServletResponse response, OutputStream outputStream) {
		
		response.setContentType("text/html");
		
		try {
			
			Source domSource = new DOMSource(createPage(request)); 
			
			Source xsltSource = new StreamSource(this.getClass().getResourceAsStream("/org/springmole/resource/xslt/main.xslt"));

			TransformerFactory transformerFactory = TransformerFactory.newInstance();

			Transformer transformer =	transformerFactory.newTransformer(xsltSource);

			transformer.transform(domSource, new StreamResult(outputStream));
		} catch (TransformerConfigurationException e) {
			e.printStackTrace();
		} catch (TransformerFactoryConfigurationError e) {
			e.printStackTrace();
		} catch (TransformerException e) {
			e.printStackTrace();
		}

		
		
	}
	
	
	private Document createPage(HttpServletRequest request) {
		String resourceParth = "file.spy?filename="; 
		String yuiFilter = "MIN";
		String yuiCombine = "true";
		
		String testEnvParam = request.getSession(true).getServletContext().getInitParameter("testEnv");
		if (testEnvParam != null && testEnvParam.equals("true")) {
			resourceParth = request.getScheme() + "://" + request.getServerName() + ":" + Integer.toString(request.getServerPort()) + request.getContextPath() + "/resource/";
		}
		
		String yuiFilterParam = request.getSession(true).getServletContext().getInitParameter("testEnv");
		if (yuiFilterParam != null && !yuiFilterParam.equals("")) {
			yuiFilter = yuiFilterParam;
		}
		
		String yuiCombineParam = request.getSession(true).getServletContext().getInitParameter("testEnv");
		if (yuiCombineParam != null && yuiCombineParam.equals("false")) {
			yuiCombine = yuiCombineParam;
		}		
		
		Document page = null;
    try {
			DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
			page = documentBuilder.newDocument();

			//create the root element and add it to the document
			Element root = page.createElement("SpringMole");
			page.appendChild(root);

			Element environment = page.createElement("Environment");
			Element path = page.createElement("Path");
			path.setAttribute("scheme", request.getScheme());
			path.setAttribute("server", request.getServerName());
			path.setAttribute("port", Integer.toString(request.getServerPort())); 
			path.setAttribute("context", request.getContextPath());
			path.setAttribute("resource", resourceParth);
			path.setAttribute("yuiFilter", yuiFilter);
			path.setAttribute("yuiCombine", yuiCombine);
			environment.appendChild(path);
			
			root.appendChild(environment);
		} catch (DOMException e) {
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		}
		
		return page;
	}
	
}
